Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_Boarding
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Public Sub Boarding_bind(ByVal oform As SAPbouiCOM.Form)
        Try

            CFL_BP_PetMaster(oform, SBO_Application)

            oEdit = oform.Items.Item("22").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("8").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("1000004").Specific
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            oform.DataBrowser.BrowseBy = "1000002"
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try

    End Sub

    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try
            If pVal.FormUID = "BoardingDogsReport" And pVal.ItemUID = "Print" And pVal.Before_Action = True And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                'puni
                Dim trd As Threading.Thread
                trd = New Threading.Thread(AddressOf Boarding_report)
                trd.IsBackground = True
                trd.SetApartmentState(ApartmentState.STA)
                trd.Start()
            End If
            '----------------

            'Choose From List
            If pVal.FormUID = "AI_BORDING" Then
                oForm = SBO_Application.Forms.Item("AI_BORDING")
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "1000004" Then
                                oEdit = oForm.Items.Item("4").Specific
                                oEdit.String = oDataTable.GetValue("U_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("1000004").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try
                            End If

                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
                If pVal.ItemUID = "10" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE Then
                    oEdit = oForm.Items.Item("10").Specific
                    If IsDate(oEdit.String) = False And oEdit.String <> "" Then
                        SBO_Application.StatusBar.SetText("Check In Not Valid Time, Plz Enter Hour:Min Time Formate", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        oEdit.Active = True
                        BubbleEvent = False
                        Exit Sub
                    End If
                End If
                If pVal.ItemUID = "29" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE Then
                    oEdit = oForm.Items.Item("29").Specific
                    If IsDate(oEdit.String) = False And oEdit.String <> "" Then
                        SBO_Application.StatusBar.SetText("Check Out Not Valid Time, Plz Enter Hour:Min Time Formate", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        oEdit.Active = True
                        BubbleEvent = False
                        Exit Sub
                    End If
                End If
                If pVal.ItemUID = "1000005" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("10").Specific
                    oEdit.String = Now.Hour.ToString() & ":" & Now.Minute.ToString()
                End If
                If pVal.ItemUID = "1000006" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("29").Specific
                    oEdit.String = Now.Hour.ToString() & ":" & Now.Minute.ToString()
                End If
                If (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE) And pVal.ItemUID = "1" And pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("22").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    oEdit = oForm.Items.Item("8").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                End If
                If (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE) And pVal.ItemUID = "1" And pVal.Before_Action = True And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    oEdit = oForm.Items.Item("1000004").Specific
                    If oEdit.String = "" Then
                        SBO_Application.StatusBar.SetText("Select Pet Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        BubbleEvent = False
                        Exit Sub
                    End If

                    oEdit = oForm.Items.Item("10").Specific
                    If IsDate(oEdit.String) = False And oEdit.String <> "" Then
                        SBO_Application.StatusBar.SetText("Check In Not Valid Time, Plz Enter Hour:Min Time Formate", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        oEdit.Active = True
                        BubbleEvent = False
                        Exit Sub
                    End If
                    oEdit = oForm.Items.Item("29").Specific
                    If IsDate(oEdit.String) = False And oEdit.String <> "" Then
                        SBO_Application.StatusBar.SetText("Check Out Not Valid Time, Plz Enter Hour:Min Time Formate", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        oEdit.Active = True
                        BubbleEvent = False
                        Exit Sub
                    End If
                    If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then

                        'oItem = oForm.Items.Item("42")
                        'If oItem.Enabled = True Then
                        oChk1 = oForm.Items.Item("42").Specific
                        If oChk1.Checked = False Then
                            BalanceBoarding_add()
                        End If
                        'End If

                        ' BalanceBoarding_add()
                    End If
                    If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                        ' BalanceBoarding_update()

                        oItem = oForm.Items.Item("42")
                        If oItem.Enabled = True Then
                            oChk1 = oForm.Items.Item("42").Specific
                            If oChk1.Checked = True Then
                                BalanceBoarding_Update_reverse()
                            Else
                                BalanceBoarding_update()
                            End If
                        End If

                    End If

                End If


            End If

        Catch ex As Exception

        End Try
    End Sub
    Function ParamDate(ByVal dt As String) As Date
        Dim d, m, y As String
        Dim Z As Date
        d = dt.Substring(0, 2)
        m = dt.Substring(3, 2)
        y = dt.Substring(6, 2)
        'Z = CDate("May 25, 1986")
        Z = CDate(d & "/" & m & "/" & y)
        'Return System.DateTime.Parse(dt, format1, Globalization.DateTimeStyles.None)
        Return Z
    End Function
    Public Sub BalanceBoarding_add()
        oEdit = oForm.Items.Item("27").Specific
        Dim NoBoard As Integer = 0
        Dim BoardUsed As Integer = 0
        Dim AccNoBaord As Integer = 0
        Dim AccBoardUsed As Integer = 0
        Dim newBoardUsed As Integer = 0
        Dim NewBoardBal As Integer = 0
        Dim NewAccBoardUsed As Integer = 0
        If oEdit.String = "" Then
            oEdit = oForm.Items.Item("1000004").Specific
            Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
            NoBoard = oRecordSet.Fields.Item(0).Value
            BoardUsed = oRecordSet.Fields.Item(1).Value
            AccNoBaord = oRecordSet.Fields.Item(3).Value
            AccBoardUsed = oRecordSet.Fields.Item(4).Value
            newBoardUsed = BoardUsed + 1
            NewBoardBal = NoBoard - newBoardUsed
            NewAccBoardUsed = AccBoardUsed + 1
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

            '===========
            If NewBoardBal < 0 Then
                Dim oODRF As SAPbobsCOM.Documents = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                oODRF.CardCode = BPCode
                oODRF.DocDate = Now.Date
                oODRF.Lines.ItemCode = "BT1002"
                oODRF.Lines.Quantity = 1
                oODRF.Lines.Add()
                Dim k As Integer
                k = oODRF.Add()
                Dim SerrorMsg As String = ""
                Ocompany.GetLastError(k, SerrorMsg)
                Dim i As Integer
                If i <> 0 Then
                    ' SBO_Application.MessageBox(Ocompany.GetLastErrorDescription, Ocompany.GetLastErrorCode)
                    'Exit Sub
                End If
            End If
            '===========
        Else
            oEdit = oForm.Items.Item("8").Specific
            Dim dt1 As Date = ParamDate(oEdit.String)
            oEdit = oForm.Items.Item("27").Specific
            Dim dt2 As Date = ParamDate(oEdit.String)
            Dim days As Integer
            days = DateDiff("d", dt1, dt2)
            If days = 0 Or days = 1 Then
                oEdit = oForm.Items.Item("1000004").Specific
                Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                NoBoard = oRecordSet.Fields.Item(0).Value
                BoardUsed = oRecordSet.Fields.Item(1).Value
                AccNoBaord = oRecordSet.Fields.Item(3).Value
                AccBoardUsed = oRecordSet.Fields.Item(4).Value
                newBoardUsed = BoardUsed + 1
                NewBoardBal = NoBoard - newBoardUsed
                NewAccBoardUsed = AccBoardUsed + 1
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

                '===========
                If NewBoardBal < 0 Then
                    Dim oODRF As SAPbobsCOM.Documents = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                    oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                    oODRF.CardCode = BPCode
                    oODRF.DocDate = Now.Date
                    oODRF.Lines.ItemCode = "BT1002"
                    oODRF.Lines.Quantity = 1
                    oODRF.Lines.Add()
                    Dim k As Integer
                    k = oODRF.Add()
                    Dim SerrorMsg As String = ""
                    Ocompany.GetLastError(k, SerrorMsg)
                    Dim i As Integer
                    If i <> 0 Then
                        ' SBO_Application.MessageBox(Ocompany.GetLastErrorDescription, Ocompany.GetLastErrorCode)
                        'Exit Sub
                    End If
                End If
                '===========

            ElseIf days > 1 Then
                oEdit = oForm.Items.Item("1000004").Specific
                Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                NoBoard = oRecordSet.Fields.Item(0).Value
                BoardUsed = oRecordSet.Fields.Item(1).Value
                AccNoBaord = oRecordSet.Fields.Item(3).Value
                AccBoardUsed = oRecordSet.Fields.Item(4).Value
                newBoardUsed = BoardUsed + days
                NewBoardBal = NoBoard - newBoardUsed
                NewAccBoardUsed = AccBoardUsed + days
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

                '===========
                If NewBoardBal < 0 Then
                    Dim oODRF As SAPbobsCOM.Documents = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDrafts)
                    oODRF.DocObjectCode = SAPbobsCOM.BoObjectTypes.oOrders
                    oODRF.CardCode = BPCode
                    oODRF.DocDate = Now.Date
                    oODRF.Lines.ItemCode = "BT1002"
                    oODRF.Lines.Quantity = 1
                    oODRF.Lines.Add()
                    Dim k As Integer
                    k = oODRF.Add()
                    Dim SerrorMsg As String = ""
                    Ocompany.GetLastError(k, SerrorMsg)
                    Dim i As Integer
                    If i <> 0 Then
                        ' SBO_Application.MessageBox(Ocompany.GetLastErrorDescription, Ocompany.GetLastErrorCode)
                        'Exit Sub
                    End If
                End If
                '===========
            End If
        End If
    End Sub
    Public Sub BalanceBoarding_Update_reverse()
        oEdit = oForm.Items.Item("27").Specific
        Dim NoBoard As Integer = 0
        Dim BoardUsed As Integer = 0
        Dim AccNoBaord As Integer = 0
        Dim AccBoardUsed As Integer = 0
        Dim newBoardUsed As Integer = 0
        Dim NewBoardBal As Integer = 0
        Dim NewAccBoardUsed As Integer = 0
        If oEdit.String = "" Then
            oEdit = oForm.Items.Item("1000004").Specific
            Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
            NoBoard = oRecordSet.Fields.Item(0).Value
            BoardUsed = oRecordSet.Fields.Item(1).Value
            AccNoBaord = oRecordSet.Fields.Item(3).Value
            AccBoardUsed = oRecordSet.Fields.Item(4).Value
            newBoardUsed = BoardUsed - 1
            NewBoardBal = NoBoard - newBoardUsed
            NewAccBoardUsed = AccBoardUsed - 1
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

        Else
            oEdit = oForm.Items.Item("8").Specific
            Dim dt1 As Date = ParamDate(oEdit.String)
            oEdit = oForm.Items.Item("27").Specific
            Dim dt2 As Date = ParamDate(oEdit.String)
            Dim days As Integer
            days = DateDiff("d", dt1, dt2)
            If days = 0 Or days = 1 Then
                oEdit = oForm.Items.Item("1000004").Specific
                Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                NoBoard = oRecordSet.Fields.Item(0).Value
                BoardUsed = oRecordSet.Fields.Item(1).Value
                AccNoBaord = oRecordSet.Fields.Item(3).Value
                AccBoardUsed = oRecordSet.Fields.Item(4).Value
                newBoardUsed = BoardUsed - 1
                NewBoardBal = NoBoard - newBoardUsed
                NewAccBoardUsed = AccBoardUsed - 1
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

              

            ElseIf days > 1 Then
                oEdit = oForm.Items.Item("1000004").Specific
                Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                NoBoard = oRecordSet.Fields.Item(0).Value
                BoardUsed = oRecordSet.Fields.Item(1).Value
                AccNoBaord = oRecordSet.Fields.Item(3).Value
                AccBoardUsed = oRecordSet.Fields.Item(4).Value
                newBoardUsed = BoardUsed - days
                NewBoardBal = NoBoard - newBoardUsed
                NewAccBoardUsed = AccBoardUsed - days
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

            End If
        End If
    End Sub
    Public Sub BalanceBoarding_update()
        Dim NoBoard As Integer = 0
        Dim BoardUsed As Integer = 0
        Dim AccNoBaord As Integer = 0
        Dim AccBoardUsed As Integer = 0
        Dim newBoardUsed As Integer = 0
        Dim NewBoardBal As Integer = 0
        Dim NewAccBoardUsed As Integer = 0
        oEdit = oForm.Items.Item("8").Specific
        Dim dt1 As Date = ParamDate(oEdit.String)
        oEdit = oForm.Items.Item("27").Specific
        Dim dt2 As Date = ParamDate(oEdit.String)
        Dim days As Integer
        days = DateDiff("d", dt1, dt2)
        If days = 0 Or days = 1 Then
            'oEdit = oForm.Items.Item("1000004").Specific
            'Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
            'oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
            'NoBoard = oRecordSet.Fields.Item(0).Value
            'BoardUsed = oRecordSet.Fields.Item(1).Value
            'AccNoBaord = oRecordSet.Fields.Item(3).Value
            'AccBoardUsed = oRecordSet.Fields.Item(4).Value
            'newBoardUsed = BoardUsed + 1
            'NewBoardBal = NoBoard - newBoardUsed
            'NewAccBoardUsed = AccBoardUsed + 1
            'oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")
        ElseIf days > 1 Then
            oEdit = oForm.Items.Item("1000004").Specific
            Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
            NoBoard = oRecordSet.Fields.Item(0).Value
            BoardUsed = oRecordSet.Fields.Item(1).Value
            AccNoBaord = oRecordSet.Fields.Item(3).Value
            AccBoardUsed = oRecordSet.Fields.Item(4).Value
            newBoardUsed = BoardUsed + days - 1
            NewBoardBal = NoBoard - newBoardUsed
            NewAccBoardUsed = AccBoardUsed + days - 1
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")
        End If
    End Sub
  
    Public Sub Boarding_report()
        oForm = SBO_Application.Forms.Item("BoardingDogsReport")
        Dim Sqlstr As String = ""
        Dim FPet As String = ""
        Dim TPet As String = ""
        Dim FDt As String = ""
        Dim TDt As String = ""
        Dim Type As String = ""
        oEdit = oForm.Items.Item("4").Specific
        If oEdit.String = "" Then
            FPet = "%"
        Else
            FPet = oEdit.String
        End If
        oEdit = oForm.Items.Item("6").Specific
        Dim yr As String
        If oEdit.String = "" Then
            yr = "2000"
            FDt = yr & "-01-01"
        Else
            FDt = oEdit.Value
        End If
        oEdit = oForm.Items.Item("8").Specific
        If oEdit.String = "" Then
            yr = Now.Year.ToString
            TDt = yr & "-12-31"
        Else
            TDt = oEdit.Value
        End If

        Try
            SBO_Application.StatusBar.SetText("Retrieving Data", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            Sqlstr = "SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_ChkDt], T0.[U_AI_ChkTime], T0.[U_AI_ChkoutDt], T0.[U_AI_ChkoutTime], T0.[U_AI_TickPass], T0.[U_AI_TickCount], T0.[U_AI_Comments], T0.[U_AI_CheckINBy], T0.[U_AI_ChkOutBy], T0.[U_AI_Remarks] FROM [dbo].[@AI_NIGHTSHIFT]  T0 WHERE T0.[U_AI_PetCode]  like '" & FPet & "'  and  T0.[U_AI_ChkDt]  between '" & FDt & "' and '" & TDt & "' order by  T0.[U_AI_ChkDt],T0.[U_AI_PetName]"
            Dim frm As MY_Report
            frm = New MY_Report
            frm.Bording_Report_Report(Sqlstr, Ocompany)
            frm.Text = "Boarding Dogs Report"
            frm.TopMost = True
            frm.Activate()
            frm.ShowDialog()
        Catch ex As Exception
        End Try

    End Sub


    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        Try

            If (pVal.MenuUID = "1288" Or pVal.MenuUID = "1289" Or pVal.MenuUID = "1290" Or pVal.MenuUID = "1291") And pVal.BeforeAction = False And pVal.InnerEvent = False Then
                Try
                    If oForm.UniqueID = "AI_BORDING" Then
                        oItem = oForm.Items.Item("42")
                        ' If oItem.Enabled = True Then
                        oChk1 = oForm.Items.Item("42").Specific
                        If oChk1.Checked = True Then
                            oItem.Enabled = False
                        Else
                            oItem.Enabled = True
                        End If
                        'End If
                    End If
                Catch ex As Exception
                End Try
            End If
            If (pVal.MenuUID = "1282" Or pVal.MenuUID = "1281") And pVal.BeforeAction = False And pVal.InnerEvent = False Then
                Try
                    If oForm.UniqueID = "AI_BORDING" Then
                        oItem = oForm.Items.Item("42")
                        oItem.Enabled = True
                    End If
                    If pVal.MenuUID = "1282" And oForm.UniqueID = "AI_BORDING" Then
                        oEdit = oForm.Items.Item("22").Specific
                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                        oEdit = oForm.Items.Item("8").Specific
                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                    End If

                Catch ex As Exception
                End Try
            End If


            oForm = SBO_Application.Forms.ActiveForm
            If oForm.UniqueID = "AI_BORDING" And pVal.BeforeAction = True And pVal.MenuUID = "1283" And pVal.InnerEvent = False Then
                oForm = SBO_Application.Forms.Item("AI_BORDING")
                oEdit = oForm.Items.Item("27").Specific
                Dim NoBoard As Integer = 0
                Dim BoardUsed As Integer = 0
                Dim AccNoBaord As Integer = 0
                Dim AccBoardUsed As Integer = 0
                Dim newBoardUsed As Integer = 0
                Dim NewBoardBal As Integer = 0
                Dim NewAccBoardUsed As Integer = 0
                If oEdit.String = "" Then
                    oEdit = oForm.Items.Item("1000004").Specific
                    Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                    oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                    NoBoard = oRecordSet.Fields.Item(0).Value
                    BoardUsed = oRecordSet.Fields.Item(1).Value
                    AccNoBaord = oRecordSet.Fields.Item(3).Value
                    AccBoardUsed = oRecordSet.Fields.Item(4).Value
                    newBoardUsed = BoardUsed - 1
                    NewBoardBal = NoBoard - newBoardUsed
                    NewAccBoardUsed = AccBoardUsed - 1
                    oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

                    '===========

                    '===========
                Else
                    oEdit = oForm.Items.Item("8").Specific
                    Dim dt1 As String = oEdit.Value
                    oEdit = oForm.Items.Item("27").Specific
                    Dim dt2 As String = oEdit.Value
                    Dim days As Integer
                    Dim st As String = "(select  cast(datediff(dd, '" & dt1 & "', '" & dt2 & "') as varchar) as 'M') "
                    oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    oRecordSet1.DoQuery(st)
                    days = oRecordSet1.Fields.Item(0).Value
                    If days = 0 Or days = 1 Then
                        oEdit = oForm.Items.Item("1000004").Specific
                        Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                        NoBoard = oRecordSet.Fields.Item(0).Value
                        BoardUsed = oRecordSet.Fields.Item(1).Value
                        AccNoBaord = oRecordSet.Fields.Item(3).Value
                        AccBoardUsed = oRecordSet.Fields.Item(4).Value
                        newBoardUsed = BoardUsed - 1
                        NewBoardBal = NoBoard - newBoardUsed
                        NewAccBoardUsed = AccBoardUsed - 1
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")



                    ElseIf days > 1 Then
                        oEdit = oForm.Items.Item("1000004").Specific
                        Dim BPCode As String = OwnerCode(oEdit.String, Ocompany)
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'")
                        NoBoard = oRecordSet.Fields.Item(0).Value
                        BoardUsed = oRecordSet.Fields.Item(1).Value
                        AccNoBaord = oRecordSet.Fields.Item(3).Value
                        AccBoardUsed = oRecordSet.Fields.Item(4).Value
                        newBoardUsed = BoardUsed - days
                        NewBoardBal = NoBoard - newBoardUsed
                        NewAccBoardUsed = AccBoardUsed - days
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_BoardBal=" & NewBoardBal & ",U_AI_BoardUsed=" & newBoardUsed & ",U_AI_AccumBoardU=" & NewAccBoardUsed & " WHERE CardCode='" & BPCode & "' ")

                        '===========
                    End If
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
